<template>
  <div class="labelManagement">
    <h3>标签管理</h3>
    <div class="search">
      <div class="cond1 cond">
        <el-input placeholder="请输入标签ID" v-model="search1">
          <template slot="prepend">标签ID</template>
        </el-input>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入标签" v-model="search2">
          <template slot="prepend">标签</template>
        </el-input>
      </div>
      <div class="cond2 cond">
        <label>状态</label>
        <el-select v-model="search3" placeholder="请选择">
          <el-option
            v-for="item in optionsStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="searchMethod">搜索</el-button>
      </div>
    </div>
    <div class="opration">
      <el-button
        type="success"
        icon="el-icon-plus"
        @click="addData"
        v-if="permissions_obj.id_86||permissions_obj.id_all"
      >添加</el-button>
    </div>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column header-align="center" align="center" prop="id" label="标签ID"></el-table-column>
        <el-table-column header-align="center" align="center" prop="title" label="标签"></el-table-column>
        <el-table-column header-align="center" align="center" prop="created_at" label="添加时间"></el-table-column>
        <el-table-column header-align="center" align="center" prop="video_count" label="视频数量"></el-table-column>
        <el-table-column header-align="center" align="center" prop="upvideo_count" label="上架视频数量"></el-table-column>
        <el-table-column header-align="center" align="center" prop="sort" label="推荐排序" sortable></el-table-column>
        <el-table-column header-align="center" align="center" label="操作" fixed="right" width="220">
          <template slot-scope="scope">
            <el-button
              type="success"
              size="mini"
              @click="handleOpen(scope.$index, scope.row)"
              v-if="scope.row.status==0"
            >打开</el-button>
            <el-button
              type="danger"
              size="mini"
              @click="handleClose(scope.$index, scope.row)"
              v-if="scope.row.status==1"
            >关闭</el-button>
            <el-button
              size="mini"
              type="warning"
              @click="handleEditor(scope.$index, scope.row)"
              v-if="permissions_obj.id_87||permissions_obj.id_all"
            >修改</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              v-if="permissions_obj.id_137||permissions_obj.id_all"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-sizes="[10,30,50,100]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize"
      ></el-pagination>
    </div>

    <!-- //绑定老用户弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="addLabelDialogVisible" width="40%" center>
      <div class="goldRow" v-if="dialogTitle =='修改标签'">
        <span>ID：</span>
        <font>{{selectTagID}}</font>
      </div>
      <div class="goldRow">
        <span>标签名称：</span>
        <el-input placeholder="请输入标签名称" v-model="inputAddLabel" clearable></el-input>
      </div>
      <div class="goldRow">
        <span>排序：</span>
        <el-input placeholder="请输入排序" v-model="inputSort" clearable type="number"></el-input>
      </div>
      <div class="dialogBtn">
        <el-button type="primary" size="small" @click="submitAddLabel">提交</el-button>
        <el-button type="info" size="small" @click="cancelAddLabel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "labelManagement",
  data() {
    return {
      search1: "",
      search2: "",
      search3: "",
      optionsStatus: [
        {
          value: "",
          label: "全部"
        },
        {
          value: 0,
          label: "禁用"
        },
        {
          value: 1,
          label: "正常"
        }
      ],
      //表格数据
      tableData: [],
      dialogTitle: "添加标签",
      addLabelDialogVisible: false,
      currentPage1: 1,
      pageSize: 10,
      totalSize: 0,
      //弹窗标签名字
      inputAddLabel: "",
      initData: "",
      //选择标签ID
      selectTagID: "",
      //弹窗排序
      inputSort: "",
      //选择行
      selectRow: "",
      // 权限管理
      permissions: [],
      permissions_obj: {}
    };
  },
  created() {
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff = false;
    this.permissions.forEach((item, index) => {
      if (item == -1) {
        oneOff = true;
      } else {
        this.permissions_obj["id_" + item] = item;
      }
    });
    if (oneOff) {
      this.permissions_obj.id_all = true;
    } else {
      this.permissions_obj.id_all = false;
    }
  },
  mounted() {
    this.getInitDataList();
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      this.currentPage1 = val;
      this.getInitDataList();
    },
    handleSizeChange(val){
      this.pageSize=val;
      this.getInitDataList();
    },
    addData() {
      this.addLabelDialogVisible = true;
      this.dialogTitle = "添加标签";
      this.inputAddLabel = "";
      this.inputSort = "";
    },
    //修改标签
    handleEditor(index, row) {
      this.addLabelDialogVisible = true;
      this.dialogTitle = "修改标签";
      this.selectRow = row;
      this.selectTagID = row.id;
      this.inputAddLabel = row.title;
      this.inputSort = row.sort;
    },
    handleOpen(index, row) {
      this.selectTagID = row.id;
      this.$confirm("是否继续该操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.modifyLabel("", "", 1, "");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消该操作"
          });
        });
    },
    handleClose(index, row) {
      this.selectTagID = row.id;
      this.$confirm("是否继续该操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.modifyLabel("", "", 0, "");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消该操作"
          });
        });
    },
    //提交添加或者修改
    submitAddLabel() {
      if (this.dialogTitle == "添加标签") {
        this.addLabel();
      } else {
        this.modifyLabel(
          this.inputAddLabel,
          "",
          this.selectRow.status,
          this.inputSort
        );
      }
    },
    //添加接口
    async addLabel() {
      let parames = {
        handshake: "",
        data: {
          title: this.inputAddLabel,
          cover: "",
          status: 1,
          sort: this.inputSort
        }
      };
      let res = await this.$axios.post(
        this.$api.tagAdd,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.getInitDataList();
        this.addLabelDialogVisible = false;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //修改接口
    async modifyLabel(title, cover, status, sort) {
      let parames = {
        handshake: "",
        data: {
          id: this.selectTagID,
          title: title,
          cover: cover,
          status: status,
          sort: sort
        }
      };
      let res = await this.$axios.post(
        this.$api.tagUpdate,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.getInitDataList();
        this.addLabelDialogVisible = false;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //取消提交
    cancelAddLabel() {
      this.addLabelDialogVisible = false;
    },
    //搜索
    searchMethod() {
      this.currentPage1 = 1;
      this.getInitDataList();
    },
    //获取初始化标签列表
    async getInitDataList() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage1,
          page_size: this.pageSize,
          search: {
            id: this.search1,
            title: this.search2,
            status: this.search3
          }
        }
      };
      let res = await this.$axios.post(
        this.$api.tagLists,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        this.totalSize = this.initData.total;
        this.tableData = this.initData.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //删除
    handleDelete(index,row){
       this.selectTagID = row.id;
      this.$confirm("是否继续该操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteMethod();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消该操作"
          });
        });
    },
    //删除接口
    async deleteMethod(){
      let parames = {
        handshake: "",
        data: {
          type_id: this.selectTagID,
        }
      };
      let res = await this.$axios.post(
        this.$api.tagDelete,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.getInitDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
  }
};
</script>
<style scoped>
.labelManagement {
  padding-bottom: 0.2rem;
}
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
.el-input {
  width: 2.5rem;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.opration {
  margin-top: 0.2rem;
  padding-left: 0.2rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.dialogBtn {
  display: flex;
  justify-content: center;
  margin-top: 0.3rem;
}
.goldRow {
  margin-bottom: 0.1rem;
}
.goldRow span {
  width: 0.9rem;
  display: inline-block;
}
</style>